package com.pktechan.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pktechan.api.entity.CloudPointFlow;
import com.pktechan.api.entity.CloudPointWithdraw;
import com.pktechan.api.entity.CustBase;
import com.pktechan.api.exception.CustomException;
import com.pktechan.api.service.CloudPointFlowService;
import com.pktechan.api.service.CloudPointService;
import com.pktechan.api.service.CloudPointWithdrawService;
import com.pktechan.api.service.RedisService;
import com.pktechan.api.utils.HtmlUtil;
import com.pktechan.api.vo.CloudPointFlowVO;
import com.pktechan.api.vo.CloudPointVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: cloudfloating
 * @description: 产品相关功能
 * @author: qx.zhang
 * @create: 2021-03-05 11:27
 **/
@RestController
@RequestMapping("cloudPointController")
public class CloudPointController {

    private static final Logger logger = LoggerFactory.getLogger(CloudPointController.class);

    @Autowired
    private CloudPointService cloudPointService;
    @Autowired
    private CloudPointFlowService cloudPointFlowService;
    @Autowired
    private CloudPointWithdrawService cloudPointWithdrawService;
    @Autowired
    private RedisService redisService;
    /**
     * 配送中心
     * @param custId
     * @param response
     */
    @RequestMapping(value = "/getDistributionCenter",method = RequestMethod.POST)
    public void getDistributionCenter(@RequestParam Integer custId, HttpServletResponse response){
        try {
            CloudPointVO cloudPointVO = cloudPointService.getDistributionCenter(custId);
            HtmlUtil.writerSuccessJson(response,cloudPointVO);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }

    /**
     * 查看收入明细列表
     * @param cloudPointFlow
     * @param response
     */
    @RequestMapping(value = "/getIncomeDetailsListByCustId",method = RequestMethod.POST)
    public void getIncomeDetailsListByCustId(@RequestBody CloudPointFlow cloudPointFlow, HttpServletResponse response){
        try {
            PageHelper.startPage(cloudPointFlow.getPageNo(), cloudPointFlow.getPageSize());
            List<CloudPointFlowVO> list = cloudPointFlowService.getIncomeDetailsListByCustId(cloudPointFlow.getCustId());
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response,pageInfo);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }
    /**
     * 查看兑付明细
     * @param cloudPointFlow
     * @param response
     */
    @RequestMapping(value = "/getCashingDetailsByCustId",method = RequestMethod.POST)
    public void getCashingDetailsByCustId(@RequestBody CloudPointFlow cloudPointFlow, HttpServletResponse response){
        try {
            PageHelper.startPage(cloudPointFlow.getPageNo(), cloudPointFlow.getPageSize());
            List<CloudPointFlowVO> list = cloudPointFlowService.getCashingDetailsByCustId(cloudPointFlow.getCustId());
            PageInfo pageInfo = new PageInfo(list);
            HtmlUtil.writerSuccessJson(response,pageInfo);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }

    /**
     * 转让兑付
     * @param cloudPointFlow
     * @param response
     */
    @RequestMapping(value = "/transferableCashing",method = RequestMethod.POST)
    public void transferableCashing(@RequestBody CloudPointFlow cloudPointFlow, HttpServletRequest request, HttpServletResponse response){
        try {
            CustBase custBase = redisService.getCustBaseByUserLoginState(request.getParameter("userLoginState"));
            cloudPointFlowService.transferableCashing(cloudPointFlow,custBase);
            HtmlUtil.writerSuccessJson(response);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }


    /**
     * 酒钻提取
     * @param response
     */
    @RequestMapping(value = "/addWithdrawPoint",method = RequestMethod.POST)
    public void addWithdrawPoint(@RequestBody CloudPointWithdraw cloudPointWithdraw, HttpServletRequest request, HttpServletResponse response){
        try {
            CustBase custBase = redisService.getCustBaseByUserLoginState(request.getParameter("userLoginState"));
            cloudPointWithdrawService.addCloudPointWithdraw(cloudPointWithdraw,custBase);
            HtmlUtil.writerSuccessJson(response);
        }catch (CustomException e) {
            e.printStackTrace();
            logger.debug(e.getErrMsg());
            HtmlUtil.writerFailJson(response, e.getErrMsg());
        }catch (Exception e){
            e.printStackTrace();
            logger.debug(e.getMessage());
            HtmlUtil.writerFailJson(response,e.getMessage());
        }
    }
}
